package com.kaifamiao.ioc.creation.test;

import com.kaifamiao.ioc.creation.Human;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Date;

public class CreationViaConstructor {

    public static void main( String[] args ) {

        //String configs = "classpath:com/kaifamiao/ioc/creation/creation-via-constructor.xml";
        String configs = "classpath*:com/**/creation-via-constructor.xml";
        // 创建 Spring 容器
        ApplicationContext container = new ClassPathXmlApplicationContext(configs);

        // 从 Spring IoC 容器获得指定名称指定类型的 bean
        Date d = container.getBean( "date", Date.class );
        System.out.println( d );

        Human h = container.getBean( "first", Human.class );
        System.out.println( h.getName() );
        System.out.println( h.getBirthdate() );

        Human x = container.getBean( "second", Human.class );
        System.out.println( x.getName() );
        System.out.println( x.getBirthdate() );

        Human y = container.getBean( "second", Human.class );
        System.out.println( x == y ); // true

        // 若从容器中获取根本不存在的 bean 则会抛出 NoSuchBeanDefinitionException
        // Human m = container.getBean( "third", Human.class );

    }

}
